var usable = false;
var pwdlegal =false;
var emaillegal =false;

function verifyuser(){
createRequest();
var name= document.getElementById("username").value;
if( name == null || name == "" ){
	usable=false;
	document.getElementById("messageusercheck").innerText="username can't be empty";
   return;
}
request.open("GET","register.verify?username="+name+"",true);
request.onreadystatechange = updatePage;
request.send(null);
}

function updatePage(){
if(request.readyState == 4 ){
if (request.getResponseHeader('usable') == 'true')
{
	usable=true;
}
   document.getElementById('messageusercheck').innerText = request.getResponseHeader('message');
}
}
function verifypassword()
{
	if(document.getElementById("password").value=="")
	{
		document.getElementById("passwordcheck").innerText="password is empty";
		pwdlegal=false;
		return;
	}
	var length=document.getElementById("password").value.length;
	if(length<5)
	{
		document.getElementById("passwordcheck").innerText="password is too short";
		pwdlegal=false;
		return;
	}
	if((document.getElementById("password").value)!=(document.getElementById("confirmpassword").value))
	{
		document.getElementById("passwordcheck").innerText="two password entry not match";
		pwdlegal=false;
		return;
	}
	document.getElementById("passwordcheck").innerText="";
	pwdlegal=true;
	return;
}

function verifyemail()
{
    var email=document.getElementById("email").value;
    if (email=="")
        emaillegal=true;
    else
    {
        var pattern=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
        emaillegal=pattern.test(email);
    }
    if (emaillegal)
		document.getElementById("emailcheck").innerText="";
    else
		document.getElementById("emailcheck").innerText="email address illegal";
}
function check()
{
	verifyuser();
	verifypassword();
	verifyemail();
	if ((usable)&&(pwdlegal)&&(emaillegal))
	{
		document.getElementById("infoerror").style.visibility="hidden";
		return true;
	}
	else
	{
		document.getElementById("infoerror").style.visibility="visible";
		return false;
	}
	return false;
}
// JavaScript Document